package com.feifan.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Donald
 * @create 2019-04-20 0:51
 * 配置fastjson 解析器
 */
@Configuration
public class FastJSON {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters()
    {
        // 1.定义一个converter消息转换对象
        FastJsonHttpMessageConverter fastJSON = new FastJsonHttpMessageConverter();
        // 2.设定一些基本配置
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 3.禁止循环引用
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect);
        fastJsonConfig.setSerializeConfig(SerializeConfig.globalInstance);
        fastJsonConfig.setDateFormat("yyyy年MM月dd日 HH:mm:ss,sss");
        fastJSON.setFastJsonConfig(fastJsonConfig);
        // 4.设置messageConverter对象对媒体的支持
        List<MediaType> list = new ArrayList<MediaType>();
        list.add(new MediaType("application","json",Charset.forName("UTF-8")));
        list.add(new MediaType("text","html",Charset.forName("UTF-8")));
        fastJSON.setSupportedMediaTypes(list);
        return new HttpMessageConverters(fastJSON);
    }
}
